home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / aga / aga023.dms / in.adf / UChess / UChessSrc.lha / dspcom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-21  |  36.8 KB  |  1,668 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. extern INTSIZE Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  25. extern char *version, *patchlevel;
  26. char mvstr[4][6];
  27. char *InPtr;
  28.  
  29. extern unsigned int TTadd;
  30.  
  31. extern int IllegalMove;
  32. int func_num=0;
  33. int thinkahead=0;
  34. int ThinkAheadWorked=0;
  35. int ThinkAheadDepth=0;
  36.  
  37. int MouseDropped=0;
  38.  
  39. #include <ctype.h>
  40. #include <signal.h>
  41.  
  42. #ifdef AMIGA
  43. #include <exec/types.h>
  44. #include <exec/exec.h>
  45. #include <proto/exec.h>
  46. #include <proto/dos.h>
  47. #include <proto/graphics.h>
  48. #include <proto/intuition.h>
  49. struct IntuiMessage __aligned globalmessage;
  50. int globalmessage_valid=0;
  51. extern struct Window __aligned *wG;
  52. extern int procpri;
  53. extern struct Process *myproc;
  54. extern struct MenuItem MenuItem6;
  55. extern struct Menu Menu1;
  56. extern unsigned char cookedchar[128];
  57. #endif
  58.  
  59. #define SIGQUIT SIGINT
  60.  
  61. #ifdef MSDOS
  62. #include <dos.h>
  63. #include <conio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <time.h>
  67. #else
  68. #include <dos.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <time.h>
  72. /*
  73. #include <sys/param.h>
  74. #include <sys/types.h>
  75. #include <sys/file.h>
  76. #include <sys/ioctl.h>
  77. */
  78. #endif
  79.  
  80.  
  81. /*
  82.  * ataks.h - Header source for GNU CHESS
  83.  *
  84.  * Copyright (c) 1988,1989,1990 John Stanback
  85.  * Copyright (c) 1992 Free Software Foundation
  86.  *
  87.  * This file is part of GNU CHESS.
  88.  *
  89.  * GNU Chess is free software; you can redistribute it and/or modify
  90.  * it under the terms of the GNU General Public License as published by
  91.  * the Free Software Foundation; either version 2, or (at your option)
  92.  * any later version.
  93.  *
  94.  * GNU Chess is distributed in the hope that it will be useful,
  95.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  96.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  97.  * GNU General Public License for more details.
  98.  *
  99.  * You should have received a copy of the GNU General Public License
  100.  * along with GNU Chess; see the file COPYING.  If not, write to
  101.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  102.  */
  103. inline int
  104. SqAtakd2 (ARGSZ int sq, ARGSZ int side)
  105.  
  106. /*
  107.  * See if any piece with color 'side' ataks sq.  First check pawns then
  108.  * Queen, Bishop, Rook and King and last Knight.
  109.  */
  110.  
  111. {
  112.   register INTSIZE u;
  113.   register unsigned char *ppos, *pdir;
  114.   INTSIZE xside;
  115.  
  116.   xside = side ^ 1;
  117.   pdir = nextdir[ptype[xside][pawn]][sq];
  118.   u = pdir[sq];            /* follow captures thread */
  119.   if (u != sq)
  120.     {
  121.       if (board[u] == pawn && color[u] == side)
  122.     return (true);
  123.       u = pdir[u];
  124.       if (u != sq && board[u] == pawn && color[u] == side)
  125.     return (true);
  126.     }
  127.   /* king capture */
  128.   if (distance (sq, PieceList[side][0]) == 1)
  129.     return (true);
  130.   /* try a queen bishop capture */
  131.   ppos = nextpos[bishop][sq];
  132.   pdir = nextdir[bishop][sq];
  133.   u = ppos[sq];
  134.   do
  135.     {
  136.       if (color[u] == neutral)
  137.     u = ppos[u];
  138.       else
  139.     {
  140.       if (color[u] == side && (board[u] == queen || board[u] == bishop))
  141.         return (true);
  142.       u = pdir[u];
  143.     }
  144.   } while (u != sq);
  145.   /* try a queen rook capture */
  146.   ppos = nextpos[rook][sq];
  147.   pdir = nextdir[rook][sq];
  148.   u = ppos[sq];
  149.   do
  150.     {
  151.       if (color[u] == neutral)
  152.     u = ppos[u];
  153.       else
  154.     {
  155.       if (color[u] == side && (board[u] == queen || board[u] == rook))
  156.         return (true);
  157.       u = pdir[u];
  158.     }
  159.   } while (u != sq);
  160.   /* try a knight capture */
  161.   pdir = nextdir[knight][sq];
  162.   u = pdir[sq];
  163.   do
  164.     {
  165.       if (color[u] == side && board[u] == knight)
  166.     return (true);
  167.       u = pdir[u];
  168.   } while (u != sq);
  169.   return (false);
  170. }
  171.  
  172.  
  173. void
  174. algbr (INTSIZE int f, INTSIZE int t, INTSIZE int flag)
  175.  
  176.  
  177. /*
  178.  * Generate move strings in different formats.
  179.  */
  180.  
  181. {
  182.   int m3p;
  183.  
  184.   if (f != t)
  185.     {
  186.       /* algebraic notation */
  187.       mvstr[0][0] = cxx[column (f)];
  188.       mvstr[0][1] = rxx[row (f)];
  189.       mvstr[0][2] = cxx[column (t)];
  190.       mvstr[0][3] = rxx[row (t)];
  191.       mvstr[0][4] = mvstr[3][0] = '\0';
  192.       if (((mvstr[1][0] = pxx[board[f]]) == 'P') || (flag & promote))
  193.     {
  194.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  195.         {
  196.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  197.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  198.           m3p = 2;
  199.         }
  200.       else
  201.         /* pawn ate */
  202.         {
  203.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  204.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  205.           mvstr[2][2] = mvstr[0][3];
  206.           m3p = 3;        /* to row */
  207.         }
  208.       if (flag & promote)
  209.         {
  210.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  211.           mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  212. #ifdef CHESSTOOL
  213.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  214.           mvstr[3][1] = mvstr[0][1];
  215.           mvstr[3][2] = mvstr[0][2];
  216.           mvstr[3][3] = mvstr[0][3];
  217.           mvstr[3][4] = '\0';
  218. #endif
  219.         }
  220.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  221.     }
  222.       else
  223.     /* not a pawn */
  224.     {
  225.       mvstr[2][0] = mvstr[1][0];
  226.       mvstr[2][1] = mvstr[0][1];
  227.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  228.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  229.       mvstr[2][4] = mvstr[1][3] = '\0';
  230.       strcpy (mvstr[3], mvstr[2]);
  231.       mvstr[3][1] = mvstr[0][0];
  232.       if (flag & cstlmask)
  233.         {
  234.           if (t > f)
  235.         {
  236.           strcpy (mvstr[1], CP[5]);
  237.           strcpy (mvstr[2], CP[7]);
  238.         }
  239.           else
  240.         {
  241.           strcpy (mvstr[1], CP[6]);
  242.           strcpy (mvstr[2], CP[8]);
  243.         }
  244.         }
  245.     }
  246.     }
  247.   else
  248.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  249. }
  250.  
  251.  
  252. int
  253. VerifyMove (char *s, INTSIZE int iop, INTSIZE unsigned int *mv)
  254.  
  255. /*
  256.  * Compare the string 's' to the list of legal moves available for the
  257.  * opponent. If a match is found, make the move on the board.
  258.  */
  259.  
  260. {
  261.   static INTSIZE pnt, tempb, tempc, tempsf, tempst, cnt;
  262.   int r,c,l;
  263.   char mystr[80];
  264.   char piece;
  265.   static struct leaf xnode;
  266.   struct leaf *node;
  267.  
  268.   if ((s[1] >= 'a') && (s[1] <= 'h'))
  269.    {
  270.     if ((s[0] == 'n') || (s[0] == 'p') || (s[0] == 'b') ||
  271.         (s[0] == 'k') || (s[0] == 'r') || (s[0] == 'q'))
  272.      {
  273.       s[0] = toupper(s[0]);
  274.      }
  275.    }
  276.   *mv = 0;
  277.   if (iop == 2)
  278.     {
  279.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  280.       return (false);
  281.     }
  282.   cnt = 0;
  283.   MoveList (opponent, 2);
  284.   pnt = TrPnt[2];
  285.   while (pnt < TrPnt[3])
  286.     {
  287.       node = &Tree[pnt++];
  288.       algbr (node->f, node->t, (INTSIZE) node->flags);
  289.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  290.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  291.     {
  292.       cnt++;
  293.       xnode = *node;
  294.     }
  295.     }
  296.   if (cnt == 1)
  297.     {
  298.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  299.       if (SqAtakd2 (PieceList[opponent][0], computer))
  300.     {
  301.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  302. #if defined CHESSTOOL
  303.       printz (CP[15]);
  304. #else
  305. #ifdef NONDSP
  306. /* Illegal move in check */
  307. #ifndef AMIGA
  308.       printz (CP[77]);
  309.       printz ("\n");
  310. #else
  311.           DisplayComputerMove(CP[77]);
  312. #endif
  313. #else
  314. /* Illegal move in check */
  315.       ShowMessage (CP[77]);
  316. #endif
  317. #endif /* CHESSTOOL */
  318.       return (false);
  319.     }
  320.       else
  321.     {
  322.       if (iop == 1)
  323.         return (true);
  324.       UpdateDisplay (xnode.f, xnode.t, 0, (INTSIZE) xnode.flags);
  325.       if ((board[xnode.t] == pawn)
  326.           || (xnode.flags & capture)
  327.           || (xnode.flags & cstlmask))
  328.         {
  329.           Game50 = GameCnt;
  330.           ZeroRPT ();
  331.         }
  332.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  333.       GameList[GameCnt].nodes = 0;
  334.       ElapsedTime (1);
  335.       GameList[GameCnt].time = (INTSIZE) et;
  336.       if (TCflag)
  337.         {
  338.           TimeControl.clock[opponent] -= et;
  339.           timeopp[oppptr] = et;
  340.           --TimeControl.moves[opponent];
  341.         }
  342.       *mv = (xnode.f << 8) | xnode.t;
  343.       algbr (xnode.f, xnode.t, false);
  344. #ifdef AMIGA
  345.       strcpy(mystr,mvstr[0]);
  346.       r = mystr[3] - '1';
  347.       c = mystr[2] - 'a';
  348.       l = ((flag.reverse) ? locn (7 - r, 7 - c) : locn (r, c));
  349.       if (color[l] == neutral)
  350.        {
  351.     DisplayBeep(0L);
  352.         Delay(25L);
  353.     DisplayBeep(0L);
  354.         Delay(25L);
  355.     DisplayBeep(0L);
  356.         Delay(25L);
  357.         piece = ' ';
  358.        }
  359.       else if (color[l] == white)
  360.         piece = qxx[board[l]]; /* white are lower case pieces */
  361.       else
  362.         piece = pxx[board[l]]; /* black are upper case pieces */
  363.       AnimateAmigaMove(mystr,piece);
  364. #endif
  365.       return (true);
  366.     }
  367.     }
  368. #if defined CHESSTOOL
  369.   printz (CP[78]);
  370. #else
  371. #ifdef NONDSP
  372. /* Illegal move */
  373. #ifdef AMIGA
  374.   sprintf(mystr,CP[75],s);
  375.   DisplayComputerMove(mystr);
  376. #else
  377.   printz (CP[75], s);
  378. #endif
  379. #ifdef DEBUG8
  380.   if (1)
  381.     {
  382.       FILE *D;
  383.       int r, c, l;
  384.       extern unsigned INTSIZE int PrVar[];
  385.       D = fopen ("/tmp/DEBUG", "a+");
  386.       pnt = TrPnt[2];
  387.       fprintf (D, "resp = %d\n", ResponseTime);
  388.       fprintf (D, "iop = %d\n", iop);
  389.       fprintf (D, "matches = %d\n", cnt);
  390.       algbr (hint >> 8, hint & 0xff, (INTSIZE) 0);
  391.       fprintf (D, "hint %s\n", mvstr[0]);
  392.       fprintf (D, "inout move is %s\n", s);
  393.       for (r = 1; PrVar[r]; r++)
  394.     {
  395.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (INTSIZE) 0);
  396.       fprintf (D, " %s", mvstr[0]);
  397.     }
  398.       fprintf (D, "\n");
  399.       fprintf (D, "legal move are \n");
  400.       while (pnt < TrPnt[3])
  401.     {
  402.       node = &Tree[pnt++];
  403.       algbr (node->f, node->t, (INTSIZE) node->flags);
  404.       fprintf (D, "%s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3]);
  405.     }
  406.       fprintf (D, "\n current board is\n");
  407.       for (r = 7; r >= 0; r--)
  408.     {
  409.       for (c = 0; c <= 7; c++)
  410.         {
  411.           l = locn (r, c);
  412.           if (color[l] == neutral)
  413.         fprintf (D, " -");
  414.           else if (color[l] == white)
  415.         fprintf (D, " %c", qxx[board[l]]);
  416.           else
  417.         fprintf (D, " %c", pxx[board[l]]);
  418.         }
  419.       fprintf (D, "\n");
  420.     }
  421.       fprintf (D, "\n");
  422.       fclose (D);
  423.       abort ();
  424.     }
  425. #endif
  426. #else
  427. /* Illegal move */
  428.   ShowMessage (CP[76]);
  429. #endif
  430. #endif /* CHESSTOOL */
  431. #if !defined CHESSTOOL && !defined XBOARD
  432.   if (cnt > 1)
  433.     ShowMessage (CP[32]);
  434. #endif /* CHESSTOOL */
  435.   return (false);
  436. }
  437.  
  438. int
  439. parser (char *f, int side)
  440. {
  441.   int c1, r1, c2, r2;
  442.  
  443.   if (f[4] == 'o')
  444.     if (side == black)
  445.       return 0x3C3A;
  446.     else
  447.       return 0x0402;
  448.   else if (f[0] == 'o')
  449.     if (side == black)
  450.       return 0x3C3E;
  451.     else
  452.       return 0x0406;
  453.   else
  454.     {
  455.       c1 = f[0] - 'a';
  456.       r1 = f[1] - '1';
  457.       c2 = f[2] - 'a';
  458.       r2 = f[3] - '1';
  459.       return (locn (r1, c1) << 8) | locn (r2, c2);
  460.     }
  461.   /*NOTREACHED*/
  462. }
  463.  
  464. int myfgets(char *buff,int len,BPTR fd)
  465. {
  466.  char tmpch;
  467.  int done=0;
  468.  int numchars=0;
  469.  int retval;
  470.  
  471.  retval = 1;
  472.  do{
  473.     if (Read(fd,&tmpch,1L) != 1L)
  474.      {
  475.       done = 1;
  476.       retval = 0;
  477.      }
  478.     else
  479.      {
  480.       buff[numchars++] = tmpch;
  481.       if (tmpch == '\n')
  482.        {
  483.         done = 1;
  484.        }
  485.       if (numchars >= (len-1))
  486.        {
  487.         done = 1;
  488.         retval = 0;
  489.        }
  490.      }
  491.   } while (!done);
  492.  buff[numchars] = '\0';
  493.  Delay(5L);
  494.  return(retval);
  495. }
  496.  
  497. void
  498. GetGame (void)
  499. {
  500.   BPTR fd;
  501.   char fname[256], *p;
  502.   int c, i, j;
  503.   INTSIZE sq;
  504.  
  505. /* enter file name */
  506.  if (!GetFileName(fname))
  507.   {
  508.    return;
  509.   }
  510.  (void)SetTaskPri((struct Task *)myproc,0);
  511.  if (fname[0] == 0)
  512.   ShowMessage (CP[63]);
  513. #ifndef AMIGA
  514.   scanz ("%s", fname);
  515. #endif
  516. /* chess.000 */
  517.   if (fname[0] == '\0')
  518.     strcpy (fname, CP[137]);
  519.   ShowMessage("Loading Game..");
  520.   Delay(20L);
  521.   if ((fd = Open (fname, MODE_OLDFILE)) != NULL)
  522.     {
  523.       Delay(20L);
  524.       NewGame ();
  525.       ShowMessage("Loading Game..");
  526.       Delay(10L);
  527.       myfgets (fname, 256, fd);
  528.       computer = opponent = white;
  529.       InPtr = fname;
  530.       skip ();
  531.       if (*InPtr == 'c')
  532.     computer = black;
  533.       else
  534.     opponent = black;
  535.       skip ();
  536.       skip ();
  537.       skip ();
  538.       Game50 = atoi (InPtr);
  539.       myfgets (fname, 256, fd);
  540.       InPtr = &fname[14];
  541.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  542.       skip ();
  543.       skip ();
  544.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  545.       myfgets (fname, 256, fd);
  546.       InPtr = &fname[11];
  547.       skipb ();
  548.       TCflag = atoi (InPtr);
  549.       skip ();
  550.       InPtr += 14;
  551.       skipb ();
  552.       OperatorTime = atoi (InPtr);
  553.       myfgets (fname, 256, fd);
  554.       InPtr = &fname[11];
  555.       skipb ();
  556.       TimeControl.clock[white] = atoi (InPtr);
  557.       skip ();
  558.       skip ();
  559.       TimeControl.moves[white] = atoi (InPtr);
  560.       myfgets (fname, 256, fd);
  561.       InPtr = &fname[11];
  562.       skipb ();
  563.       TimeControl.clock[black] = atoi (InPtr);
  564.       skip ();
  565.       skip ();
  566.       TimeControl.moves[black] = atoi (InPtr);
  567.       myfgets (fname, 256, fd);
  568.       for (i = 7; i > -1; i--)
  569.     {
  570.       myfgets (fname, 256, fd);
  571.       p = &fname[2];
  572.       InPtr = &fname[11];
  573.       skipb ();
  574.       for (j = 0; j < 8; j++)
  575.         {
  576.           sq = i * 8 + j;
  577.           if (*p == '.')
  578.         {
  579.           board[sq] = no_piece;
  580.           color[sq] = neutral;
  581.         }
  582.           else
  583.         {
  584.           for (c = 0; c < 8; c++)
  585.             {
  586.               if (*p == pxx[c])
  587.             {
  588.               board[sq] = c;
  589.               color[sq] = black;
  590.             }
  591.             }
  592.           for (c = 0; c < 8; c++)
  593.             {
  594.               if (*p == qxx[c])
  595.             {
  596.               board[sq] = c;
  597.               color[sq] = white;
  598.             }
  599.             }
  600.         }
  601.           p++;
  602.           Mvboard[sq] = atoi (InPtr);
  603.           skip ();
  604.         }
  605.     }
  606.       GameCnt = 0;
  607.       flag.regularstart = true;
  608.       Book = BOOKFAIL;
  609.       myfgets (fname, 256, fd);
  610.       myfgets (fname, 256, fd);
  611.       myfgets (fname, 256, fd);
  612.       while (myfgets (fname, 256, fd))
  613.     {
  614.       struct GameRec *g;
  615.       int side = computer;
  616.  
  617. /*printf("in while loop\n");*/
  618.       side = side ^ 1;
  619.       ++GameCnt;
  620.       InPtr = fname;
  621.       skipb ();
  622.       g = &GameList[GameCnt];
  623.       g->gmove = parser (InPtr, side);
  624.       skip ();
  625.       g->score = atoi (InPtr);
  626.       skip ();
  627.       g->depth = atoi (InPtr);
  628.       skip ();
  629.       g->nodes = atoi (InPtr);
  630.       skip ();
  631.       g->time = atoi (InPtr);
  632.       skip ();
  633.       g->flags = c = atoi (InPtr);
  634.       skip ();
  635.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  636.       skip ();
  637.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  638.       g->piece = no_piece;
  639.       g->color = neutral;
  640.       if (c & (capture | cstlmask))
  641.         {
  642.           if (c & capture)
  643.         {
  644.           skip ();
  645.           for (c = 0; c < 8; c++)
  646.             if (pxx[c] == *InPtr)
  647.               break;
  648.           g->piece = c;
  649.         }
  650.           skip ();
  651.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  652.         }
  653.     }
  654.       /* GameCnt--; */
  655.       if (TimeControl.clock[white] > 0)
  656.     TCflag = true;
  657.       Close (fd);
  658.     }
  659.   ZeroRPT ();
  660.   InitializeStats ();
  661.   UpdateDisplay (0, 0, 1, 0);
  662.   Sdepth = 0;
  663.   hint = 0;
  664.   ShowMessage("Game Loaded");
  665. #ifdef AMIGA
  666.   DrawAmigaBoard();
  667. #endif
  668.   (void)SetTaskPri((struct Task *)myproc,procpri);
  669. }
  670.  
  671. void
  672. GetXGame (void)
  673. {
  674.   FILE *fd;
  675.   char fname[256], *p;
  676.   int c, i, j;
  677.   INTSIZE sq;
  678. /* Enter file name */
  679.   ShowMessage (CP[63]);
  680. #ifndef AMIGA
  681.   scanz ("%s", fname);
  682. #endif
  683.   if (fname[0] == '\0')
  684. /* xboard.position.read*/
  685.     strcpy (fname, CP[205]);
  686.   if ((fd = fopen (fname, "r")) != NULL)
  687.     {
  688.       NewGame ();
  689.       flag.regularstart = false;
  690.       Book = false;
  691.       fgets (fname, 256, fd);
  692.       fname[6] = '\0';
  693.       if (strcmp (fname, CP[206]))
  694.     return;
  695.       fgets (fname, 256, fd);
  696.       fgets (fname, 256, fd);
  697.       for (i = 7; i > -1; i--)
  698.     {
  699.       fgets (fname, 256, fd);
  700.       p = fname;
  701.       for (j = 0; j < 8; j++)
  702.         {
  703.           sq = i * 8 + j;
  704.           if (*p == '.')
  705.         {
  706.           board[sq] = no_piece;
  707.           color[sq] = neutral;
  708.         }
  709.           else
  710.         {
  711.           for (c = 0; c < 8; c++)
  712.             {
  713.               if (*p == qxx[c])
  714.             {
  715.               board[sq] = c;
  716.               color[sq] = black;
  717.             }
  718.             }
  719.           for (c = 0; c < 8; c++)
  720.             {
  721.               if (*p == pxx[c])
  722.             {
  723.               board[sq] = c;
  724.               color[sq] = white;
  725.             }
  726.             }
  727.         }
  728.           p += 2;
  729.         }
  730.     }
  731.       fclose (fd);
  732.     }
  733.   ZeroRPT ();
  734.   InitializeStats ();
  735.   UpdateDisplay (0, 0, 1, 0);
  736.   Sdepth = 0;
  737.   hint = 0;
  738. }
  739.  
  740. void
  741. SaveGame (void)
  742. {
  743.   FILE *fd;
  744.   char fname[256];
  745.   INTSIZE sq, i, c, f, t;
  746.   char p;
  747.  
  748.   if (!(GetFileName(savefile)))   
  749.    { 
  750.     return;
  751.    }
  752.   (void)SetTaskPri((struct Task *)myproc,0);
  753.   if (savefile[0])
  754.     strcpy (fname, savefile);
  755.   else
  756.     {
  757. /* Enter file name*/
  758.       ShowMessage (CP[63]);
  759. #ifndef AMIGA
  760.       scanz ("%s", fname);
  761. #endif
  762.     }
  763.  
  764.   if (fname[0] == '\0')
  765. /* chess.000 */
  766.     strcpy (fname, CP[137]);
  767.   Delay(25L);
  768.   if ((fd = fopen (fname, "w")) != NULL)
  769.     {
  770.       char *b, *w;
  771.  
  772.       Delay(25L);
  773.       b = w = CP[74];
  774.       if (computer == black)
  775.     b = CP[141];
  776.       if (computer == white)
  777.     w = CP[141];
  778.       fprintf (fd, CP[37], b, w, Game50);
  779.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  780.       fprintf (fd, CP[111], TCflag, OperatorTime);
  781.       fprintf (fd, CP[117],
  782.            TimeControl.clock[white], TimeControl.moves[white],
  783.            TimeControl.clock[black], TimeControl.moves[black]);
  784.       for (i = 7; i > -1; i--)
  785.     {
  786.       fprintf (fd, "%1d ", i + 1);
  787.       for (c = 0; c < 8; c++)
  788.         {
  789.           sq = i * 8 + c;
  790.           switch (color[sq])
  791.         {
  792.         case black:
  793.           p = pxx[board[sq]];
  794.           break;
  795.         case white:
  796.           p = qxx[board[sq]];
  797.           break;
  798.         default:
  799.           p = '.';
  800.         }
  801.           fprintf (fd, "%c", p);
  802.         }
  803.       for (f = i * 8; f < i * 8 + 8; f++)
  804.         fprintf (fd, " %d", Mvboard[f]);
  805.       fprintf (fd, "\n");
  806.     }
  807.       fprintf (fd, "  %s\n", cxx);
  808.       fprintf (fd, CP[126]);
  809.       for (i = 1; i <= GameCnt; i++)
  810.     {
  811.       struct GameRec *g = &GameList[i];
  812.  
  813.       f = g->gmove >> 8;
  814.       t = (g->gmove & 0xFF);
  815.       algbr (f, t, g->flags);
  816.       fprintf (fd, "%s %5d %5d %7ld %5d %5d  %#08lx %#08lx  %c   %s\n",
  817.            mvstr[0], g->score, g->depth,
  818.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,
  819.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]));
  820.     }
  821.       fclose (fd);
  822. /* Game saved */
  823.       ShowMessage (CP[70]);
  824.     }
  825.   else
  826.     /*ShowMessage ("Could not open file");*/
  827.     ShowMessage (CP[48]);
  828.   (void)SetTaskPri((struct Task *)myproc,procpri);
  829. }
  830.  
  831. void
  832. ListGame (getstr)
  833. int getstr;
  834. {
  835.   FILE *fd;
  836.   INTSIZE i, f, t;
  837.   long when;
  838.   char fname[256], dbuf[256];
  839.  
  840.   if (listfile[0])
  841.     strcpy (fname, listfile);
  842.   else
  843.     {
  844. #ifdef MSDOS
  845.       sprintf (fname, "chess.lst");
  846. #else
  847.       if (!getstr)
  848.        {
  849.         time (&when);
  850.         strncpy (dbuf, ctime (&when), 20);
  851.         dbuf[7] = '\0';
  852.         dbuf[10] = '\0';
  853.         dbuf[13] = '\0';
  854.         dbuf[16] = '\0';
  855.         dbuf[19] = '\0';
  856. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  857.    date is Jan 1
  858.    time is 02:03:04
  859.    program played black */
  860.         sprintf (fname, "t:UC%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  861.         /* replace space padding with 0 */
  862.         for (i = 0; fname[i] != '\0'; i++)
  863.         if (fname[i] == ' ')
  864.         fname[i] = '0';
  865.        }
  866.       else
  867.        {
  868.         if (!GetFileName(fname))
  869.          {
  870.           return;
  871.          }
  872.        }
  873. #endif /* MSDOS */
  874.     }
  875.   Delay(5L);
  876.   (void)SetTaskPri((struct Task *)myproc,0);
  877.   fd = fopen (fname, "w");
  878.   Delay(5L);
  879.   if (!fd)
  880.     {
  881.      (void)SetTaskPri((struct Task *)myproc,procpri);
  882.      return;
  883.     }
  884.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  885.   fprintf (fd, "%s\n", VERSTRING);
  886.   fprintf (fd, CP[10]);
  887.   fprintf (fd, CP[11]);
  888.   for (i = 1; i <= GameCnt; i++)
  889.     {
  890.       f = GameList[i].gmove >> 8;
  891.       t = (GameList[i].gmove & 0xFF);
  892.       algbr (f, t, GameList[i].flags);
  893.       if(GameList[i].flags & book)
  894.           fprintf (fd, "%5s  %5d    Book%7ld %5d", mvstr[0],
  895.            GameList[i].score, 
  896.            GameList[i].nodes, GameList[i].time);
  897.       else
  898.           fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  899.            GameList[i].score, GameList[i].depth,
  900.            GameList[i].nodes, GameList[i].time);
  901.       if ((i % 2) == 0)
  902.     {
  903.       fprintf (fd, "\n");
  904. #ifdef DEBUG40
  905.       if (computer == black)
  906.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  907.              GameList[i].d1,
  908.              GameList[i].d2,
  909.              GameList[i].d3,
  910.              GameList[i].d4,
  911.              GameList[i].d5,
  912.              GameList[i].d6,
  913.              GameList[i].d7);
  914.       else
  915.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  916.              GameList[i - 1].d1,
  917.              GameList[i - 1].d2,
  918.              GameList[i - 1].d3,
  919.              GameList[i - 1].d4,
  920.              GameList[i - 1].d5,
  921.              GameList[i - 1].d6,
  922.              GameList[i - 1].d7);
  923. #endif
  924.     }
  925.       else
  926.     fprintf (fd, "         ");
  927.     }
  928.   fprintf (fd, "\n\n");
  929.   if (GameList[GameCnt].flags & draw)
  930.     {
  931.       fprintf (fd, CP[54], DRAW);
  932.     }
  933.   else if (GameList[GameCnt].score == -9999)
  934.     {
  935.       fprintf (fd, "%s\n", ColorStr[player ]);
  936.     }
  937.   else if (GameList[GameCnt].score == 9998)
  938.     {
  939.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  940.     }
  941.   fclose (fd);
  942.   (void)SetTaskPri((struct Task *)myproc,procpri);
  943. }
  944.  
  945. void
  946. Undo (void)
  947.  
  948. /*
  949.  * Undo the most recent half-move.
  950.  */
  951.  
  952. {
  953.   INTSIZE f, t;
  954.   f = GameList[GameCnt].gmove >> 8;
  955.   t = GameList[GameCnt].gmove & 0xFF;
  956.   if (board[t] == king && distance (t, f) > 1)
  957.     (void) castle (GameList[GameCnt].color, f, t, 2);
  958.   else
  959.     {
  960.       /* Check for promotion: */
  961.       if (GameList[GameCnt].flags & promote)
  962.     {
  963.       board[t] = pawn;
  964.     }
  965.       board[f] = board[t];
  966.       color[f] = color[t];
  967.       board[t] = GameList[GameCnt].piece;
  968.       color[t] = GameList[GameCnt].color;
  969.       if (color[t] != neutral)
  970.     Mvboard[t]--;
  971.       Mvboard[f]--;
  972.     }
  973.   if (GameList[GameCnt].flags & epmask)
  974.     EnPassant (otherside[color[f]], f, t, 2);
  975.   else
  976.     InitializeStats ();
  977.   if (TCflag && (TCmoves>1))
  978.     ++TimeControl.moves[color[f]];
  979.   hashkey = GameList[GameCnt].hashkey;
  980.   hashbd = GameList[GameCnt].hashbd;
  981.   GameCnt--;
  982.   computer = computer ^ 1;
  983.   opponent = opponent ^ 1;
  984.   flag.mate = false;
  985.   Sdepth = 0;
  986.   player = player ^ 1;
  987.   ShowSidetoMove ();
  988.   UpdateDisplay (0, 0, 1, 0);
  989.   if (flag.regularstart)
  990.     Book = BOOKFAIL;
  991. }
  992.  
  993. void
  994.  TestSpeed (void (*f) ( INTSIZE int side, INTSIZE int ply), unsigned j)
  995. {
  996.   char astr[256];
  997.   INTSIZE i;
  998.   long cnt, rate, t1, t2;
  999.  
  1000.   t1 = time (0);
  1001.   Forbid();
  1002.   for (i = 0; i < j; i++)
  1003.     {
  1004.       f (opponent, 2);
  1005.     }
  1006.   Permit();
  1007.   t2 = time (0);
  1008.   cnt = j * (TrPnt[3] - TrPnt[2]);
  1009.   if (t2 - t1)
  1010.     et = (t2 - t1) * 100;
  1011.   else
  1012.     et = 1;
  1013.   rate = (et) ? (cnt / et) : 0;
  1014.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  1015. #ifdef NONDSP
  1016. #ifdef AMIGA
  1017. if (!func_num)
  1018.  sprintf(astr,"Mlst=%dN/s",rate*100);
  1019. else if (func_num == 1)
  1020.  sprintf(astr,"Clst=%dN/s",rate*100);
  1021. ShowMessage(astr);
  1022. #else
  1023.   printz (CP[91], cnt, rate*100);
  1024. #endif
  1025. #ifdef DEBUG9
  1026.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  1027.     {
  1028.       struct leaf *node = &Tree[j];
  1029.       algbr (node->f, node->t, node->flags);
  1030.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  1031.     }
  1032. #endif
  1033. #else
  1034.   ShowNodeCnt (cnt);
  1035. #endif
  1036. }
  1037.  
  1038. void
  1039.  TestPSpeed (INTSIZE int (*f) (INTSIZE int side), unsigned j)
  1040. {
  1041.   char astr[256];
  1042.   INTSIZE i;
  1043.   long cnt, rate, t1, t2;
  1044.  
  1045.   t1 = time (0);
  1046.   Forbid();
  1047.   for (i = 0; i < j; i++)
  1048.     {
  1049.       (void) f (opponent);
  1050.     }
  1051.   Permit();
  1052.   t2 = time (0);
  1053.   cnt = j;
  1054.   if (t2 - t1)
  1055.     et = (t2 - t1) * 100;
  1056.   else
  1057.     et = 1;
  1058.   rate = (et) ? (cnt / et) : 0;
  1059.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  1060. #ifdef NONDSP
  1061. #ifdef AMIGA
  1062. sprintf(astr,"Eval=%ldN/s",rate*100);
  1063. ShowMessage(astr);
  1064. #else
  1065.   printz (CP[91], cnt, rate*100);
  1066. #endif
  1067. #else
  1068.   ShowNodeCnt (cnt);
  1069. #endif
  1070. }
  1071.  
  1072.  
  1073. void
  1074. SetMachineTime (char *s)
  1075. {
  1076.   char *time;
  1077.   int m, t;
  1078.   time = &s[strlen (CP[197])];
  1079.   m = strtol (time, &time, 10);
  1080.   t = strtol (time, &time, 10);
  1081.   if (t)
  1082.     TimeControl.clock[computer] = t;
  1083.   if (m)
  1084.     TimeControl.moves[computer] = m;
  1085. #ifdef XBOARD
  1086.   printz (CP[222], m, t);
  1087. #endif
  1088. }
  1089.  
  1090.  
  1091. void
  1092. InputCommand (cstring)
  1093.  
  1094. char *cstring;
  1095.  
  1096. /*
  1097.  * Process the users command. If easy mode is OFF (the computer is thinking
  1098.  * on opponents time) and the program is out of book, then make the 'hint'
  1099.  * move on the board and call SelectMove() to find a response. The user
  1100.  * terminates the search by entering ^C (quit siqnal) before entering a
  1101.  * command. If the opponent does not make the hint move, then set Sdepth to
  1102.  * zero.
  1103.  */
  1104.  
  1105. {
  1106.   char tstr[40];
  1107.   int i = 0;
  1108.   INTSIZE have_shown_prompt = false;
  1109.   INTSIZE ok, tmp;
  1110.   unsigned INTSIZE mv;
  1111.   char s[80], sx[80];
  1112.  
  1113. #if defined CHESSTOOL
  1114.   INTSIZE normal = false;
  1115.  
  1116. #endif
  1117.  
  1118.   ok = flag.quit = false;
  1119.   player = opponent;
  1120.   ft = 0;
  1121. #ifdef CACHE
  1122.   if(TTadd > ttblsize)ZeroTTable();
  1123. #endif
  1124.   if (hint > 0 && !flag.easy && !flag.force)
  1125.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  1126.       {
  1127.         thinkahead = 1;
  1128.     fflush (stdout);
  1129.         ft = time0;
  1130.     /*time0 = time ((long *) 0);*/
  1131.     algbr ((INTSIZE) hint >> 8, (INTSIZE) hint & 0x3F, false);
  1132.     strcpy (s, mvstr[0]);
  1133.     tmp = epsquare;
  1134. #ifdef DEBUG12
  1135.     if (1)
  1136.       {
  1137.         FILE *D;
  1138.         int r, c, l;
  1139.         extern unsigned INTSIZE int PrVar[];
  1140.         D = fopen ("/tmp/DEBUGA", "a+");
  1141.         fprintf (D, "score = %d\n", root->score);
  1142.         fprintf (D, "inout move is %s\n", s);
  1143.         for (r = 1; PrVar[r]; r++)
  1144.           {
  1145.         algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (INTSIZE) 0);
  1146.         fprintf (D, " %s", mvstr[0]);
  1147.           }
  1148.         fprintf (D, "\n current board is\n");
  1149.         for (r = 7; r >= 0; r--)
  1150.           {
  1151.         for (c = 0; c <= 7; c++)
  1152.           {
  1153.             l = locn (r, c);
  1154.             if (color[l] == neutral)
  1155.               fprintf (D, " -");
  1156.             else if (color[l] == white)
  1157.               fprintf (D, " %c", qxx[board[l]]);
  1158.             else
  1159.               fprintf (D, " %c", pxx[board[l]]);
  1160.           }
  1161.         fprintf (D, "\n");
  1162.           }
  1163.         fprintf (D, "\n");
  1164.         fclose (D);
  1165.       }
  1166. #endif
  1167. #if !defined CHESSTOOL
  1168. #ifndef AMIGA
  1169.     if (flag.post)
  1170.       GiveHint ();
  1171. #endif
  1172. #endif
  1173.     if (VerifyMove (s, 1, &mv))
  1174.       {
  1175.         Sdepth = 0;
  1176. #ifdef QUIETBACKGROUND
  1177. #ifdef NONDSP
  1178.         PromptForMove ();
  1179. #else
  1180.         ShowSidetoMove ();
  1181.         ShowPrompt ();
  1182. #endif
  1183.         have_shown_prompt = true;
  1184. #endif /* QUIETBACKGROUND */
  1185.         SelectMove (computer, 2);
  1186.         VerifyMove (s, 2, &mv);
  1187.         Sdepth = 0;
  1188.       }
  1189.     /*ft = (time ((long *) 0) - time0) * 100;*/
  1190.     epsquare = tmp;
  1191.     time0 = ft;
  1192.       }
  1193.   while (!(ok || flag.quit))
  1194.     {
  1195. #if defined CHESSTOOL
  1196.       normal = false;
  1197. #endif
  1198.       player = opponent;
  1199. #ifdef QUIETBACKGROUND
  1200.       if (!have_shown_prompt)
  1201.     {
  1202. #endif /* QUIETBACKGROUND */
  1203. #ifdef NONDSP
  1204.       PromptForMove ();
  1205. #else
  1206.       ShowSidetoMove ();
  1207.       ShowPrompt ();
  1208. #endif
  1209. #ifdef QUIETBACKGROUND
  1210.     }
  1211.       have_shown_prompt = false;
  1212. #endif /* QUIETBACKGROUND */
  1213. #ifdef NONDSP
  1214.       s[0] = sx[0] = '\0';
  1215. #ifndef AMIGA
  1216.       while (!sx[0])
  1217.     i = (int) gets (sx);
  1218. #else
  1219.        thinking2 = 0;
  1220.        i = 1;
  1221.        thinkahead = 0;
  1222.        GetOperatorEntry(sx);
  1223. #endif
  1224. #else
  1225.       fflush (stdout);
  1226.       i = (int) getstr (sx);
  1227. #endif
  1228.       sscanf (sx, "%s", s);
  1229.       if (i == EOF)
  1230.     ExitChess ();
  1231.       if (s[0] == '\0')
  1232.     continue;
  1233.       if (strcmp (s, CP[131]) == 0)    /*bd*/
  1234.     {
  1235. #if defined CHESSTOOL || defined XBOARD
  1236.       chesstool = 0;
  1237. #endif /* CHESSTOOL */
  1238.       ClrScreen ();
  1239.       UpdateDisplay (0, 0, 1, 0);
  1240. #if defined CHESSTOOL || defined XBOARD
  1241.       chesstool = 1;
  1242. #endif /* CHESSTOOL */
  1243.     }
  1244.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1245.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1246.     flag.quit = true;
  1247.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1248.     {
  1249.      /* flag.post = !flag.post;*/
  1250.     }
  1251.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1252.     EditBoard ();
  1253. #ifdef NONDSP
  1254.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1255.     SetupBoard ();
  1256. #endif
  1257.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1258.     {
  1259. #if defined CHESSTOOL
  1260.       computer = white;
  1261.       opponent = black;
  1262.       flag.force = false;
  1263.       Sdepth = 0;
  1264. #endif /* CHESSTOOL */
  1265.       ok = true;
  1266.     }
  1267.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1268.     {
  1269.       ok = true;
  1270.       flag.force = false;
  1271.       if (computer == white)
  1272.         {
  1273.           computer = black;
  1274.           opponent = white;
  1275.         }
  1276.       else
  1277.         {
  1278.           computer = white;
  1279.           opponent = black;
  1280.         }
  1281.     }
  1282.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1283.     help ();
  1284.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1285.     flag.material = !flag.material;
  1286.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1287.     {flag.force = !flag.force; flag.bothsides = false;}
  1288.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1289.     Book = Book ? 0 : BOOKFAIL;
  1290.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1291.     {
  1292.       NewGame ();
  1293.       UpdateDisplay (0, 0, 1, 0);
  1294.     }
  1295.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1296.     ListGame (0xff);
  1297.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1298.        {
  1299.         GetTimeString(tstr);
  1300.     SelectLevel (tstr);
  1301.        }
  1302.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1303.     flag.hash = !flag.hash;
  1304.       else if (strcmp (s, CP[132]) == 0)    /*beep*/
  1305.     flag.beep = !flag.beep;
  1306.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1307.     {
  1308.       SetMachineTime (sx);
  1309.     }
  1310.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1311.     ChangeAlphaWindow ();
  1312.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1313.     ChangeBetaWindow ();
  1314.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1315.     flag.rcptr = !flag.rcptr;
  1316.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1317.        {
  1318.     GiveHint ();
  1319.        }
  1320.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1321.     {
  1322.       flag.bothsides = !flag.bothsides;
  1323.           if (flag.bothsides)
  1324.            {
  1325.             (void)SetTaskPri((struct Task *)myproc,0);
  1326.             thinkahead = 1;
  1327.            }
  1328.           flag.force = false;
  1329.       Sdepth = 0;
  1330.       ElapsedTime (1);
  1331.       SelectMove (opponent, 1);
  1332.       ok = true;
  1333.     }
  1334.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1335.     {
  1336. #ifndef AMIGA
  1337.       flag.reverse = !flag.reverse;
  1338.       ClrScreen ();
  1339.       UpdateDisplay (0, 0, 1, 0);
  1340. #endif
  1341.     }
  1342.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1343.     {
  1344.       computer = computer ^ 1;
  1345.       opponent = opponent ^ 1;
  1346.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1347.       flag.force = false;
  1348.       Sdepth = 0;
  1349.       ok = true;
  1350.     }
  1351.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1352.     {
  1353.       computer = black;
  1354.       opponent = white;
  1355.       xwndw = WXWNDW;
  1356.       flag.force = false;
  1357.       Sdepth = 0;
  1358.  
  1359.       /*
  1360.            * ok = true; don't automatically start with white command
  1361.            */
  1362.     }
  1363.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1364.     {
  1365.       computer = white;
  1366.       opponent = black;
  1367.       xwndw = BXWNDW;
  1368.       flag.force = false;
  1369.       Sdepth = 0;
  1370.  
  1371.       /*
  1372.            * ok = true; don't automatically start with black command
  1373.            */
  1374.     }
  1375.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1376.     {
  1377. #ifndef AMIGA
  1378.       Undo ();
  1379. #endif
  1380.     }
  1381.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1382.     {
  1383. #ifndef AMIGA
  1384.       Undo ();
  1385.       Undo ();
  1386. #endif
  1387.     }
  1388.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1389.     GetGame ();
  1390.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1391.     GetXGame ();
  1392.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1393.     SaveGame ();
  1394.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1395.     ChangeSearchDepth ();
  1396. #ifdef DEBUG
  1397.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1398.     ChangeDbLev ();
  1399. #endif /* DEBUG */
  1400.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1401.     ChangeHashDepth ();
  1402.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1403.     dither = DITHER;
  1404.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1405.        {
  1406.     /*flag.easy = !flag.easy;*/
  1407.     flag.easy = flag.easy; /* mod this for menu toggle on amiga */
  1408.        }
  1409.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1410.     SetContempt ();
  1411.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1412.     ChangeXwindow ();
  1413.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1414.     {
  1415.       flag.rv = !flag.rv;
  1416.       UpdateDisplay (0, 0, 1, 0);
  1417.     }
  1418.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1419.     {
  1420.       flag.coords = !flag.coords;
  1421.       UpdateDisplay (0, 0, 1, 0);
  1422.     }
  1423.       else if (strcmp (s, CP[193]) == 0)    /*stras*/
  1424.     {
  1425.       flag.stars = !flag.stars;
  1426.       UpdateDisplay (0, 0, 1, 0);
  1427.     }
  1428.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1429.     {
  1430. /*      ShowMessage (CP[108]);/*test movelist*/
  1431.           ShowMessage ("Testing..");
  1432.           func_num = 0;
  1433.       TestSpeed (MoveList, 20000);
  1434. /*      ShowMessage (CP[107]);/*test capturelist*/
  1435.           func_num++;
  1436.       TestSpeed (CaptureList, 30000);
  1437. /*      ShowMessage (CP[85]);/*test score position*/
  1438.           func_num++;
  1439.       TestPSpeed (ScorePosition, 15000);
  1440.     }
  1441.       else
  1442.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1443.     ShowPostnValues ();
  1444.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1445.     DoDebug ();
  1446.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1447.         DoTable (Mwpawn);
  1448.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1449.         DoTable (Mbpawn);
  1450.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1451.         DoTable (Mknight[white]);
  1452.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1453.         DoTable (Mknight[black]);
  1454.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1455.         DoTable (Mbishop[white]);
  1456.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1457.         DoTable (Mbishop[black]);
  1458.       else
  1459.     { /* this is where we move the humans pieces */
  1460. #if defined CHESSTOOL
  1461.       normal = (ok = VerifyMove (s, 0, &mv));
  1462. #else
  1463.       ok = VerifyMove (s, 0, &mv);
  1464. #endif
  1465. #ifdef OLDVERSION1_01
  1466.       if ((ok && mv != hint))
  1467.         {
  1468.           Sdepth = 0;
  1469.           ft = 0;
  1470.         }
  1471.       else
  1472.         Sdepth = 0;
  1473. #else
  1474.       if ((!flag.easy)&&(mv == hint))
  1475.            {
  1476.             ThinkAheadDepth = Sdepth2;
  1477.             ThinkAheadWorked = 1;
  1478.            }
  1479.           else
  1480.            {
  1481.             ThinkAheadDepth = 0;
  1482.             ThinkAheadWorked = 0;
  1483.            }
  1484.       Sdepth = 0;
  1485. #endif
  1486.           if ((!ok))
  1487.            {
  1488.             IllegalMove = 1;
  1489.            }
  1490.           if ((ok)&&(MouseDropped))
  1491.            {
  1492.                 DoLegalMove(s);
  1493.                 MouseDropped = 0;
  1494.            }
  1495.     }
  1496.     }
  1497.  
  1498.   ElapsedTime (1);
  1499.   if (flag.force)
  1500.     {
  1501.       computer = opponent;
  1502.       opponent = computer ^ 1;
  1503.     }
  1504. #if defined CHESSTOOL || defined XBOARD
  1505. #if defined CHESSTOOL
  1506.   if (normal)
  1507.     if (computer == white)
  1508.       printz ("%d. %s", ++mycnt2, s);
  1509.     else
  1510.       printz ("%d. ... %s", ++mycnt2, s);
  1511. #else
  1512.   printz ("%d. %s\n", ++mycnt2, s);
  1513. #endif
  1514. #ifdef notdef
  1515.   if (flag.post)
  1516.     {
  1517.       REG int i;
  1518.  
  1519.       printz (" %6d ", MSCORE);
  1520.       for (i = 1; MV[i] > 0; i++)
  1521.     {
  1522.       algbr ((INTSIZE) (MV[i] >> 8), (INTSIZE) (MV[i] & 0xFF), false);
  1523.       printz ("%5s ", mvstr[0]);
  1524.     }
  1525.     }
  1526.   printz ("\n");
  1527. #endif
  1528. #endif /* CHESSTOOL */
  1529.   signal (SIGINT, TerminateSearch);
  1530. #ifndef MSDOS
  1531.   signal (SIGQUIT, TerminateSearch);
  1532. #endif /* MSDOS */
  1533. }
  1534.  
  1535.  
  1536. void
  1537. ElapsedTime (INTSIZE int iop)
  1538.  
  1539.  
  1540. /*
  1541.  * Determine the time that has passed since the search was started. If the
  1542.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1543.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1544.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1545.  */
  1546.  
  1547. {
  1548. #ifndef MSDOS
  1549.   extern int errno;
  1550. #ifdef AMIGA
  1551.    struct IntuiMessage *localmessage;
  1552.    long __aligned class,code;
  1553. #endif
  1554. #ifdef FIONREAD
  1555.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1556.     {
  1557.       perror ("FIONREAD");
  1558.       fprintf (stderr,
  1559.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1560.     i, errno, FIONREAD);
  1561.       exit (1);
  1562.     }
  1563.  
  1564.   if (nchar)
  1565.     {
  1566.       if (!flag.timeout)
  1567.     flag.back = true;
  1568.       flag.bothsides = false;
  1569.     }
  1570. #endif /*FIONREAD*/
  1571. #ifdef AMIGA /* check if I need to interrupt thinking */
  1572.   if (thinkahead)
  1573.   {
  1574.   while ( (localmessage = (struct IntuiMessage *)
  1575.     GetMsg(wG->UserPort) )) /* got a message at window port */
  1576.    {
  1577.         if (localmessage->Class == RAWKEY)
  1578.          {
  1579.           if (localmessage->Code < 56)
  1580.            code = cookedchar[localmessage->Code];
  1581.           else
  1582.            code = 0;
  1583.          }
  1584.         else
  1585.          {
  1586.           code = 'A';
  1587.          }
  1588.         if (isalpha(code))
  1589.          {
  1590.           (void)SetTaskPri((struct Task *)myproc,procpri);
  1591.           if (!flag.timeout)
  1592.            {
  1593.             flag.back = true;
  1594.            }
  1595.           flag.bothsides = false;
  1596.       globalmessage_valid = 0xffff;
  1597.           globalmessage = *localmessage;
  1598.          }
  1599.     ReplyMsg((struct Message *)localmessage);
  1600.    }
  1601.   }
  1602.   else if (thinking2) /* check for move now menu item */
  1603.   {
  1604.   while ( (localmessage = (struct IntuiMessage *)
  1605.     GetMsg(wG->UserPort) )) /* got a message at window port */
  1606.    {
  1607.     class = localmessage->Class;
  1608.         code = localmessage->Code;
  1609.     ReplyMsg((struct Message *)localmessage);
  1610.         if ((class == MENUPICK))
  1611.           {
  1612.         if (ItemAddress(&Menu1,code) == &MenuItem6)
  1613.             {
  1614.              if (!flag.timeout)
  1615.               {
  1616.                flag.back = true;
  1617.               flag.musttimeout = true;
  1618.               }
  1619.              flag.bothsides = false;
  1620.             }
  1621.           }
  1622.    }
  1623.   }
  1624. #endif
  1625. #else
  1626.   if (kbhit ())
  1627.     {
  1628.       if (!flag.timeout)
  1629.     flag.back = true;
  1630.       flag.bothsides = false;
  1631.     }
  1632. #endif /* MSDOS */
  1633.   et = (time ((long *) 0) - time0) * 100;
  1634.   ETnodes = NodeCnt + ZNODES;
  1635.   if (et < 0)
  1636.     et = 0;
  1637.   if (iop == 1)
  1638.     {
  1639.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1640.     flag.timeout = true;
  1641.       ETnodes = NodeCnt + ZNODES;
  1642.       time0 = time ((long *) 0);
  1643.     }
  1644. #ifdef AMIGA
  1645.     UpdateClocks ();
  1646. #endif
  1647. }
  1648.  
  1649.  
  1650. void
  1651. SetTimeControl (void)
  1652. {
  1653.   if (TCflag)
  1654.     {
  1655.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1656.       TimeControl.clock[white] = 6000L * TCminutes + TCseconds * 100;
  1657.       TimeControl.clock[black] = 6000L * TCminutes + TCseconds * 100;
  1658.     }
  1659.   else
  1660.     {
  1661.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1662.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  1663.     }
  1664.   flag.onemove = (TCmoves == 1);
  1665.   et = 0;
  1666.   ElapsedTime (1);
  1667. }
  1668.